package com.utils
{
	/**
	 * 将图片序列帧转换为动画 
	 */	
	import flash.display.Sprite;
	
	public class BitmapClipFrameAssets extends Sprite
	{
		private var copyBitmap:CopyBitmap;
		private var _rows:int;
		private var _cols:int;
		private var _dealy:int;
		
		public function BitmapClipFrameAssets(path:String,rows:int=1,cols:int=1,dealy:int = 30,loadOver:Function = null){
			_rows = rows;
			_cols = cols;
			_dealy = dealy;
			copyBitmap = new CopyBitmap(this,path,_rows,_cols,_dealy,null,"auto",false,loadOver);
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */		
		public function gotoAndStopRow(value:int = 1):void
		{
			if(!copyBitmap) return;
			if(value > _rows) value = _rows;
			copyBitmap.offsetRow = value - 1;
		}
		
		public function gotoAndStopCol(value:int = 1):void{
			if(!copyBitmap) return;
			if(value > _cols) value = _cols;
			copyBitmap.offsetCol = value - 1;
		}
		
		public function dispose():void{
			copyBitmap.dispose();
			while(this.numChildren) removeChildAt(0);
			copyBitmap = null;
			this.parent.removeChild(this);
		}
		
		
		
		
		
		
		
		
		public function get cols():int
		{
			return _cols;
		}
		
		public function get rows():int
		{
			return _rows;
		}
		
		override public function get height():Number{
			return copyBitmap.height;
		}
		
	}
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.clearInterval;
import flash.utils.setInterval;

class CopyBitmap extends Bitmap{
	
	private var _loader:Loader;
	private var _bmp:Bitmap;
	private var _bmd:BitmapData;
	private var _bmdWidth:Number = 0;
	private var _bmdHeight:Number = 0;
	private var _copyData:BitmapData;
	
	private var _interval:int;
	
	private var _index:int;
	private var _offsetRow:int;
	private var _offsetCol:int = -1;
	
	private var _contain:Sprite;
	private var _rows:int;
	private var _cols:int;
	private var _dealy:int;
	private var _path:String;
	private var _callBack:Function = new Function();
	
	/**
	 * @param contain 父容器
	 * @param path 路径
	 * @param rows 行数
	 * @param cols 列数
	 * @param dealy 间隔时间 间隔时间设为0则不执行循环，不为0则按间隔时间循环执行
	 * @param bitmapData 默认值
	 * @param pixelSnapping 默认值
	 * @param smoothing 默认值
	 * @param callBack 加载完后外部要执行的函数
	 */	
	public function CopyBitmap(contain:Sprite,path:String,rows:int,cols:int,dealy:int = 30,bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false,callBack:Function = null)
	{
		super(bitmapData, pixelSnapping, smoothing);
		_contain = contain;
		_contain.addChild(this);
		
		_path = path;
		_rows = rows;
		_cols = cols;
		_dealy = dealy;
		_callBack = callBack;
		getRoleData(_path);
	}
	
	private function getRoleData(path:String):void{
		_loader = new Loader();
		_loader.load(new URLRequest(path));
		_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIO_ERRORHandler);
		_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
	}
	
	/**错误捕捉*/	
	protected function onIO_ERRORHandler(e:IOErrorEvent):void
	{
		trace('路径加载出错:' + _path);
	}

	/**加载完毕后执行*/	
	private function onCompleteHandler(e:Event):void
	{
		if(!_loader) return;
		_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onIO_ERRORHandler);
		_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteHandler);
		
		_bmp = _loader.content as Bitmap;
		_bmd = _bmp.bitmapData;
		_bmp.smoothing = true;//平滑处理
		
		_bmdWidth = _bmp.width / _cols;
		_bmdHeight = _bmp.height / _rows;
		
		_copyData = new BitmapData(_bmdWidth,_bmdHeight,true,0);
		
		//如果没有间隔时间，则执行一次，否则按间隔时间循环执行
		_dealy == 0 ? loopData() : _interval = setInterval(loopData,_dealy);
		
		this.x = -_bmdWidth/2;
		this.y = -_bmdHeight;
		
		if(_callBack is Function) _callBack();
	}		
	
	/**循环函数,替换bitmapData对象实现动画效果*/	
	private function loopData():void{
		//拷贝指定区域像素
		_copyData.copyPixels(_bmd,new Rectangle((_offsetCol == -1 ? (_index % _cols) : _offsetCol) * _bmdWidth,_offsetRow * _bmdHeight,_bmdWidth,_bmdHeight),new Point());
		this.bitmapData = _copyData;
		_index ++;
	}
	
	/**
	 * 停止 
	 */	
	public function stopInterval():void{
		clearInterval(_interval);
	}
	
	/**
	 * 指定循环行数
	 * @param value
	 */	
	public function set offsetRow(value:int):void
	{
		_offsetRow = value;
	}
	
	public function set offsetCol(value:int):void
	{
		_offsetCol = value;
	}
	
	
	override public function get height():Number{
		return _bmdHeight;
	}
	
	override public function get width():Number{
		return _bmdWidth;
	}
	
	public function dispose():void{
		stopInterval();
		if(this.parent) this.parent.removeChild(this);
		_copyData = null;
		_bmd = null;
		_bmp = null;
		_loader.unloadAndStop(true);
		_loader = null;
			
	}
}